<?php
namespace app\vueapi\controller;

use app\common\model\{
    Loss as LossModel, Laboratory as LaboratoryModel
};

use think\Db;

class Loss extends Base
{
    public function index()
    {
        $data = $this->request->param();
        $page = $data['pageSize'] ?? 10;
        $loss = LossModel::find()->paginate($page);
        $Laboratory = LaboratoryModel::field('id , name')->select();
        $res = $loss;
        foreach($loss as $key => $value){
            // $res[$key] = $value;
            foreach($Laboratory as $key1 => $value1){
                if($value['laboratory_id'] == $value1['id']) {
                    $res[$key]['laboratory_id'] = $value1['name'];
                };
            };
        };
        return json(msg(1, $res));
    }

    public function addLoss()
    {
        $data = $this->request->param();
        if(!empty($data['laboratory_id']) || !empty($data['service']) || !empty($data['consumables']) ){
            $res = LossModel::where('laboratory_id',$data['laboratory_id'])->select();
            if($res != []) {
                return json(msg(0, '', '该实验室已存在学生！'));
            }else{
                $res = LossModel::insert($data);
                return returnRes($res,'添加'); 
            }
        }else {
            return json(msg(0, '', '值不能为空'));
        }
    }

    public function changeLoss()
    {
        $data = $this->request->param();
        //不允许有相同的属性
        $loss = LossModel::where('id', $data['id'])->find();
        if(!empty($data['laboratory_id']) || !empty($data['service']) || !empty($data['consumables'])){
            $loss->consumables  = $data['consumables'];
            $loss->service  = $data['service'];
            $loss -> save();
            return returnRes($loss, '修改');
        }
        return json(msg(0, '', '该实验室不存在'));
    }
}

?>